  import 'package:flutter/material.dart';

/*
  在 IOS 上使用 WillPopScope , 无论返回 true 还是 false, 都会让系统自带的从左往右滑返回的手势失效
  如果我们不想让手势失效,那么可以这么用:

  不返回的时候传函数, 需要返回的时候传 null
  ```
  onWillPop: _locked ? () async => false : null,
  ```

*/

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
          appBar: AppBar(
            title: Text('Material App Bar'),
          ),
          body: Builder(builder: (ctx) {
            return Center(
              child: TextButton(
                child: Text('跳转'),
                onPressed: () {
                  Navigator.push(ctx, MaterialPageRoute(builder: (ctx1) {
                    return HomePage();
                  }));
                },
              ),
            );
          })),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _locked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('第二个界面'),
      ),
      body: WillPopScope(
        onWillPop: _locked ? () async => false : null,
        child: Center(
          child: TextButton(
            child: Text(_locked ? "锁住了" : "解锁啦"),
            onPressed: () {
              setState(() {
                _locked = !_locked;
              });
            },
          ),
        ),
      ),
    );
  }
}
